package org.proxycine.gestsalles.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.proxycine.gestsalles.model.entities.Cinema;


public final class CinemaDao extends Adapter {

	private static CinemaDao instance;

	private CinemaDao() {
		instance = this;
	}

	public static CinemaDao getInstance() {
		if (instance == null)
			instance = new CinemaDao();

		return instance;
	}

	public List<Cinema> getAllCinemas() {
		List<Cinema> cinemas = new ArrayList<Cinema>();

		String request = "SELECT * from Cinema";
		try {
			ResultSet rs = getStatement().executeQuery(request);

			while (rs.next()) {
				cinemas.add(new Cinema(rs.getInt("id"), rs.getString("nom"), rs
						.getString("adresse"), rs.getString("ville"), rs
						.getString("code_postal")));
			}
			
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cinemas;
	}
	
	public Cinema getCinema(String nom)
	{
		Cinema cinema = null;
		String request = "SELECT * " +
				"FROM Cinema C " +
				"WHERE C.id = '" + nom + "'";
				
				try {
					ResultSet rs = getStatement().executeQuery(request);
					
					if (rs.next()) {
						cinema = new Cinema(rs.getInt("id"),
								rs.getString("nom"),
								rs.getString("adresse"),
								rs.getString("ville"),
								rs.getString("code_postal"),
								EmployeDao.getInstance().getEmployeFromLogin(rs.getString("gerant")));
					}
					
					rs.close();
					
				} catch (SQLException e) {
					e.printStackTrace();
				}		
		
		return cinema;
	}

	public Cinema getCinemaOf(String loginGerant)
	{
		Cinema cinema = null;
		String request = "SELECT * " +
				"FROM Cinema C " +
				"WHERE C.gerant = '" + loginGerant + "'";
				
				try {
					ResultSet rs = getStatement().executeQuery(request);
					
					if (rs.next()) {
						cinema = new Cinema(rs.getInt("id"),
								rs.getString("nom"),
								rs.getString("adresse"),
								rs.getString("ville"),
								rs.getString("code_postal"),
								EmployeDao.getInstance().getEmployeFromLogin(rs.getString("gerant")));
					}
					
					rs.close();
					
				} catch (SQLException e) {
					e.printStackTrace();
				}		
		
		return cinema;
	}
	
	public Cinema getCinema(int id)
	{
		Cinema cinema = null;
		String request = "SELECT * " +
				"FROM Cinema C " +
				"WHERE C.id = '" + id + "'";
				
				try {
					ResultSet rs = getStatement().executeQuery(request);
					
					if (rs.next()) {
						cinema = new Cinema(rs.getInt("id"),
								rs.getString("nom"),
								rs.getString("adresse"),
								rs.getString("ville"),
								rs.getString("code_postal"),
								EmployeDao.getInstance().getEmployeFromLogin(rs.getString("gerant")));
					}
					
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}		
		
		return cinema;
	}
}
